package com.xjx.controller;

import com.xjx.common.Result;
import com.xjx.enmus.EmailStatus;
import com.xjx.service.EMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@RestController
public class EmailController {

    @Autowired
    private EMailService eMailService;

    @PostMapping("/sendEmail")
    public Result<String> sendEmail(@RequestBody Map<String, String> requestBody) {

        String to = requestBody.get("to");
        String subject = requestBody.get("subject");
        String content = requestBody.get("content");

        EmailStatus status = eMailService.sendMail(to, subject, content);

        if (status == EmailStatus.SUCCESS) {
            return Result.success("邮件发送成功");
        } else {
            return Result.failure("邮件发送失败");
        }
    }


    @PostMapping("/sendEmailAttachments")
    public Result<String> sendEmailWithAttachments(
            @RequestParam("to") String to,
            @RequestParam("subject") String subject,
            @RequestParam("content") String content,
            @RequestPart("attachments") MultipartFile[] attachments) {

        if (to == null || to.isEmpty() || subject == null || subject.isEmpty() || content == null || content.isEmpty()) {
            return Result.failure("邮件参数不完整");
        }

        EmailStatus status = eMailService.sendMailWithAttachments(to, subject, content, attachments);

        if (status == EmailStatus.SUCCESS) {
            return Result.success("带多个附件的邮件发送成功");
        } else {
            return Result.failure("带多个附件的邮件发送失败");
        }
    }

}

